using System;
using Sce.PlayStation.Core;

namespace Nyanimals
{
	public class CollisionBox : CollisionShape
	{
        CMesh boxMesh;
        
		private Vector3 position;
		private float width;
        private float height;
        private float depth;
        
		
        public CMesh BoxMesh
        {
            get { return boxMesh;}
        }
        
#if SHOWCOLLISION
		CStaticContext boxContext;
        public CStaticContext Context
        {
            get { return boxContext;}
            set { boxContext = value;}
        }
#endif
		
		public override Vector3 Position
		{
			get { return position;}
			set 
			{ 
				position = value;
#if SHOWCOLLISION
				boxContext.WorldMatrix = BuildMatrix();		
#endif
			}
		}
        public float Width
        {
            get { return width;}
        }
        public float Height
        {
            get { return height;}
        }
        public float Depth
        {
            get { return depth;}
        }
		public override bool Draw
		{
			get { return bDraw;}
			set 
            {
                bDraw = value;
                
#if SHOWCOLLISION
                boxContext.Active = value;
#endif
            }
		}
		
		private Matrix4 BuildMatrix()
		{
			Matrix4 mat = Matrix4.Translation(position);
			Matrix4 scl = Matrix4.Scale(width, height, depth);
			
			return mat * scl;
		}
		
        /// <summary>
        /// Initializes a new instance of the <see cref="Nyanimals.CollisionBox"/> class.
        /// </summary>
        /// <param name='pos'>
        /// Position.
        /// </param>
        /// <param name='w'>
        /// Width
        /// </param>
        /// <param name='h'>
        /// Height
        /// </param>
        /// <param name='d'>
        /// Depth
        /// </param>
        public CollisionBox(Vector3 pos, float w, float h, float d)
        {
            position = pos;
            width = w;
            height = h;
            depth = d;
            
            boxMesh = new CMesh();
            boxMesh.MakeBox(w, h, d);
            
            
#if SHOWCOLLISION
            // load in the box mesh
            int meshIndex = CMeshManager.Instance.CheckMesh(boxMesh.Name);
            
            if(meshIndex == -1)
            {
                meshIndex = CMeshManager.Instance.AddMesh(boxMesh);
            }
            int contextIndex = CContextManager.Instance.AddStaticContext(meshIndex, BuildMatrix());
            
            boxContext = (CStaticContext)CContextManager.Instance.GetContext(contextIndex);
            
            boxContext.Active = bDraw;           
            boxContext.WireFrame = true;
#endif
        }
        ~CollisionBox()
        {
        }
		
        public override void Clear()
        {
            
            
#if SHOWCOLLISION
            if(boxContext != null)
            {
                CContextManager.Instance.RemoveContext(boxContext);
                boxContext = null;
            }
#endif
        }
        
		public override CollisionTypes Type 
		{
			get { return CollisionTypes.BOX;}
		}
		
		public override void Render ()
		{
            
		}

		public override bool Collides (ref CollisionShape other)
		{
			switch(other.Type)
			{
             case CollisionTypes.SPHERE:
             {
             } break;                
             case CollisionTypes.BOX:
             {
             } break;                
			};
			
			return false;
		}
		
		
		private bool BoxToBoxCollisionCheck(ref CollisionBox other)
		{
			
			return false;
		}
	}
}

